/*
   Copyright (c) [2023] [魔凤啸天]
   [MFUI] is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details.  
*/
#include "checkbutton.hpp"

namespace MFUI{
	void CheckButton::_released(Window* w,SDL_FPoint p){check();released(w,p);}
	void CheckButton::check(){value=!value;redraw=true;notifyparent();_changed();}
	CheckButton::CheckButton(SDL_Color back,bool v):Button(),background(back),value(v){}
	CheckButton::~CheckButton(){}
	void CheckButton::changed(){}
	void CheckButton::_changed(){changed();}
	bool CheckButton::getvalue()const{return value;}
	void CheckButton::setvalue(bool v){if(value!=v)check();}
	void CheckButton::setbackground(SDL_Color c){background=c,redraw=true,notifyparent();}
	SDL_Color CheckButton::getbackground()const{return background;}
	void CheckButton::draw(Renderer* r,bool updatecache){
		_draw(r,updatecache);
		auto back=background.a;
		switch(getstate()){
		  case state_released:break;
		  case state_on:back>>=1;break;
		  case state_pressed:back>>=2;break;
		}
		r->setdrawcolor({background.r,background.g,background.b,back});
		r->fillrectF(&rect);
		redraw=false;
		if(value){
			auto w=rect.w,h=rect.h,hw=w*0.5F,hh=h*0.5F,x=rect.x,y=rect.y;
			r->setdrawcolor({(Uint8)~background.r,(Uint8)~background.g,(Uint8)~background.b,back});
			r->drawline(x,y+hh,x+hw,y+h);
			r->drawline(x+hw,y+h,x+w,y);
		}
		makecircle(r,rect);
	}
	void CheckButton::mouseentered(Window* w){redraw=true;notifyparent();}
	void CheckButton::mouseexited(Window* w){redraw=true;notifyparent();}
	void CheckButton::_losefocus(Window* w){redraw=true;notifyparent();}
	void CheckButton::_pressed(Window* w,SDL_FPoint p){redraw=true;notifyparent();pressed(w,p);}
}